package com.lbs.jiepiaisi.biz;

import android.content.Intent;

import com.lbs.jiepiaisi.bean.AnswerQuestionBean;
import com.lbs.jiepiaisi.manager.App;
import com.lbs.jiepiaisi.parser.HomeQuestionParser;
import com.lbs.jiepiaisi.utils.Constant;
import com.lbs.jiepiaisi.utils.HttpUtil;
import com.lbs.jiepiaisi.utils.UrlUtil;

import okhttp3.FormBody;
import okhttp3.Response;

/**
 * 时间：2017/8/28 16:24
 * 作用：查询问题列表
 */

public class QuestionHomeBiz {

    public void questionBiz(String visType, final String flag) {
        FormBody formBody = new FormBody
                .Builder()
                .add("visId", "1")//设备编号
                .add("visType", visType)//开始时间
                .build();
        HttpUtil.getHttpUtil().post(UrlUtil.ACCOUNT_HOME_QUESTION, formBody, new HttpUtil.Listener() {
            @Override
            public void success(Response response) {
                AnswerQuestionBean answerQuestionBean = new AnswerQuestionBean();
                try {
                    //返回结果
                    String jsonString = response.body().string();
                    answerQuestionBean = HomeQuestionParser.parser(jsonString);
                    answerQuestionBean.setStatus(Constant.STATUS_OK);
                } catch (Exception e) {
                    answerQuestionBean.setStatus(Constant.STATUS_NETWORK_ERROR);
                    e.printStackTrace();
                } finally {
                    Intent intent = new Intent(flag);
                    intent.putExtra(Constant.KEY_DATA, answerQuestionBean);
                    App.instance.sendBroadcast(intent);
                }
            }

            @Override
            public void failed() {
                AnswerQuestionBean answerQuestionBean = new AnswerQuestionBean();
                answerQuestionBean.setStatus(Constant.STATUS_NETWORK_ERROR);
                Intent intent = new Intent(flag);
                intent.putExtra(Constant.KEY_DATA, answerQuestionBean);
                App.instance.sendBroadcast(intent);
            }

            @Override
            public void noNetwork() {
                AnswerQuestionBean answerQuestionBean = new AnswerQuestionBean();
                answerQuestionBean.setStatus(Constant.STATUS_NO_NET);
                Intent intent = new Intent(flag);
                intent.putExtra(Constant.KEY_DATA, answerQuestionBean);
                App.instance.sendBroadcast(intent);
            }
        });
    }
}
